﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharpzuoye
{
        //Logger作业
        //控制台项目中：
        //能够在EF core上配置成功Logger到Debug窗口
    public class SqlDbContext : DbContext
    {
        //public DbSet<User> Users { get; set; }
        //public DbSet<EmailMessage> EmailMessages { get; set; }
        public DbSet<Content> Contents { get; set; }
        public DbSet<Problem> Problems { get; set; }
        public DbSet<Article> Articles { get; set; }
        public DbSet<Suggest> Suggests { get; set; }
        public DbSet<User> Users { get; set; }

        public DbSet<HelpMoney> HelpMoneys { get; set; }
        public DbSet<Repoistory> Repoistories { get; set; }
        public DbSet<Message> Messages { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string connectionstring =
                @"Data Source=(localdb)\ProjectsV13;Initial Catalog=17bang;Integrated Security=True;";
            optionsBuilder.UseSqlServer(connectionstring)
#if DEBUG
                .EnableSensitiveDataLogging()

#endif
                .LogTo((id,level) => level == Microsoft.Extensions.Logging.LogLevel.Debug,
                log => Console.WriteLine(log)
                );
        }

        //自定义配置作业
        //分别使用OnModelCreating()和Data Annotations，完成以下配置：
        //将之前的User类名改为Register，但仍然能对应表User
        //将之前的User属性Name改成UserName，但仍然能对应表User的列Name
        //将Name的长度限制为256
        //Password不能为空
        //将User表的主键设置在Name列上
        //User类中的属性FailedTry不用存储到数据库中
        //给CreateTime属性添加一个非聚集唯一索引
        //CreateTime不能小于2000年1月1日
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //modelBuilder.Entity<User>().HasBaseType((Type)null);
            //modelBuilder.Entity<EmailMessage>().HasBaseType((Type)null);

            //modelBuilder.Entity<User>()
            //    .HasOne<EmailMessage>(m => m.Message)
            //    .WithOne()
            //    .HasForeignKey<User>(m => m.MessageId);
                

            modelBuilder.Entity<User>(s =>
            {
                //s.HasKey(m => m.Id);
                //s.ToTable("Register");
                //s.Property(m => m.Name).HasMaxLength(256)
                //.HasColumnName("UserName");
                //s.HasKey(m => m.Name);
                //s.Property(m => m.Password).IsRequired();
                //s.Ignore(m => m.FailedTry);
                //s.HasIndex(m => m.CreateTime).IsUnique();
                s.HasCheckConstraint("CK_CreateTime", "CreateTime > '2000-1-1'");

            });

        }
            
    }
        
    
}
